package com.maizhi.util;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

/**
 * IP工具类
 * 
 * @author 孙宇
 * 
 */
public class IpUtil {

	/**
	 * 获取登录用户IP地址
	 * 
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (ip.equals("0:0:0:0:0:0:0:1")) {
			ip = "本地";
		}
		return ip;
	}
	
	public final static String getPro(HttpServletRequest request) 
	   {
	   	String pro = "";
	   	String p = "&";
	   	 StringBuffer sb = new StringBuffer();


	   	 Enumeration names = request.getParameterNames();
	    	while (names.hasMoreElements()) 
	    	{
	     	String name = (String) names.nextElement();

	         try {
	        	if (request.getParameter(name) != null
	                && request.getParameter(name).toString().length() >0)  
	        		{
	        		
	        		sb.append("&");
	        		sb.append(name);
	   			sb.append("=");
	   			sb.append(request.getParameter(name));
	        		} 

	         } catch (Exception e) {
	         e.printStackTrace();
	         }
	    	}
	    	return sb.toString();
	  }	
	
	public static String getRemortIP(HttpServletRequest request)
	  {
	  	  if (request.getHeader("x-forwarded-for") == null) {
	  	   return request.getRemoteAddr();
	  	  } else
	  	  return request.getHeader("x-forwarded-for");
	  }

}
